package com.wanmait.shoppingsys.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.shoppingsys.util.DbHelper;
import com.wanmait.shoppingsys.vo.Admin;
import com.wanmait.shoppingsys.vo.City;
import com.wanmait.shoppingsys.vo.Specialty;
import com.wanmait.shoppingsys.vo.Type;

public class AdminDAO {

	private static AdminDAO adminDAO;
	
	private AdminDAO() {}
		public static AdminDAO getDAO() {
			if(adminDAO==null) {
				adminDAO=new AdminDAO();
			}
		return adminDAO;
	}
	
	 public ArrayList<Admin>findAll()
	 {
		 ArrayList params=new ArrayList();
		 String sql="SELECT * FROM administrator";
		 Connection conn=DbHelper.getConn();
		 PreparedStatement ps=null;
		 ResultSet rs=null;
		 
		 try {
			 ps=conn.prepareStatement(sql);
			 for(int i=0;params!=null&&i<params.size();i++) {
					ps.setObject(i+1, params.get(i));
				}
			 rs = ps.executeQuery();
			 while(rs.next())
			 {
				 Admin admin=new Admin();
				 admin.setId(rs.getInt("id"));
				 admin.setRealName(rs.getString("realname"));
				 admin.setName(rs.getString("name"));
				 admin.setActor(rs.getString("actor"));
				 admin.setEnable(rs.getBoolean("enable"));
				 params.add(admin);
			 }
		 }catch(SQLException e) {
				
				e.printStackTrace();
			}
		    finally {
		       DbHelper.close(rs, ps, conn);
		    }
		 return params;
	 }
	 
	 public void insert(Admin admin) {
		 String sql="INSERT into administrator(name,pass,realname,actor)VALUES(?,?,?,?)";
		 
		 ArrayList params=new ArrayList();
		 params.add(admin.getName());
		 params.add(admin.getPass());
		 params.add(admin.getRealName());
		 params.add(admin.getActor());
		 DbHelper.update(sql, params);
	 }
	 
	 public Admin findByNameAndPass(String name,String pass) {
		 Admin admin=null;
		 String sql="SELECT * FROM administrator WHERE name=?  AND pass=? ";
		 Connection conn=DbHelper.getConn();
		 PreparedStatement ps=null;
		 ResultSet rs=null;
		 try {
			 ps=conn.prepareStatement(sql);
			 ps.setString(1,name);
			 ps.setString(2,pass);
			 
			 rs=ps.executeQuery();
			 if(rs.next()) {
				 admin=new Admin();
				 admin.setName(rs.getString("name"));
				 admin.setPass(rs.getString("pass"));
				 admin.setEnable(rs.getBoolean("enable"));
				 admin.setRealName(rs.getString("realname"));
				 admin.setActor(rs.getString("actor"));
			 }
		 }catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DbHelper.close(rs, ps, conn);
			}
		 return admin;
	 }
	 
	 public Admin findpass(String name,String realname) {
		 Admin admin=null;
		 String sql="SELECT　* FROM administrator WHERE name=? and realname=?";
		 Connection conn=DbHelper.getConn();
		 PreparedStatement ps=null;
		 ResultSet rs=null;
		 
		 try {
			 ps=conn.prepareStatement(sql);
			 ps.setString(1, name);
			 ps.setString(2, realname);
			 rs=ps.executeQuery();
			 if(rs.next()) {
				 admin=new Admin();
				 admin.setName(rs.getString("name"));
				 admin.setPass(rs.getString("pass"));
				 admin.setId(rs.getInt("id"));
			 }
		 } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DbHelper.close(rs, ps, conn);
			}
		 return admin;
	 }
	 
	 public void updatePass(Integer id,String pass) {
		 String sql="UPDATE　administrator SET pass=? WHERE id=?";
		 ArrayList params=new ArrayList();
		 params.add(pass);
		 params.add(id);
		 
		 DbHelper.update(sql, params);
	 }
	 
	 
	 public void delete(Integer id) {
			String sql = "DELETE FROM  administrator WHERE id=?";
			ArrayList params = new ArrayList();
			params.add(id);
			DbHelper.update(sql,params);
		}
	 
	 public void update1(Integer id,Integer en) {//逻辑删除管理员
			String sql = "UPDATE administrator SET enable=? where id=?";
			ArrayList params = new ArrayList();
			params.add(en);
			params.add(id);
			DbHelper.update(sql,params);
		}
	 
	 
	 public Admin findById(Integer id)   // 通过ID查询
		{
			Admin admin = null;
			String sql = "SELECT * from administrator WHERE id=?";
			Connection conn=DbHelper.getConn();
			PreparedStatement ps = null;
			ResultSet rs=null;
			try {
				ps = conn.prepareStatement(sql);
				ps.setInt(1, id);
				rs = ps.executeQuery();
				while(rs.next())
				{
					admin = new Admin();
					admin.setId(rs.getInt("id"));
					admin.setRealName(rs.getString("realname"));
					admin.setName(rs.getString("name"));	
					admin.setPass(rs.getString("pass"));
				}
		
			} catch (Exception e) {
				e.printStackTrace();
			}
			finally {
				DbHelper.close(rs,ps,conn);
			}
			return admin;	
		}
	 
	 
	 public void update(Admin admin) {
			String sql = "UPDATE administrator SET realname=?,name=?,pass=? where id=?";
			ArrayList params = new ArrayList();
			params.add(admin.getRealName());
			params.add(admin.getName());
			params.add(admin.getPass());
			params.add(admin.getId());
			DbHelper.update(sql,params);
		}
	 
	 
}
